﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using Maticsoft.Common;
using LTP.Accounts.Bus;
namespace XDSoft.Web.TB_Options
{
    public partial class Add : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.ddlParent.AppendDataBoundItems = true;
                XDSoft.BLL.TB_Options bll = new XDSoft.BLL.TB_Options();
                this.ddlParent.DataSource = bll.GetModelList("isSys=1");
                this.ddlParent.DataTextField = "name";
                this.ddlParent.DataValueField = "code";
                this.ddlParent.DataBind();
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {

            string strErr = "";
            if (this.txtcode.Text.Trim().Length == 0)
            {
                strErr += "分类编码不能为空！\\n";
            }
            if (this.txtname.Text.Trim().Length == 0)
            {
                strErr += "分类名称不能为空！\\n";
            }
            if (this.ddlParent.SelectedItem == null)
            {
                strErr += "上级分类不能为空！\\n";
            }
            if (!PageValidate.IsNumber(txtsort.Text))
            {
                strErr += "分类排序格式错误！\\n";
            }

            if (strErr != "")
            {
                MessageBox.Show(this, strErr);
                return;
            }
            string type = "SysType";
            string code = this.txtcode.Text;
            string name = this.txtname.Text;
            int sort = int.Parse(this.txtsort.Text);
            bool isSys = this.chkisSys.Checked;

            XDSoft.BLL.TB_Options bll = new XDSoft.BLL.TB_Options();
            if (bll.GetRecordCount("parent='" + this.ddlParent.SelectedItem.Value + "' and code='" + code + "'") > 0)
            {
                MessageBox.Show(this, "分类【" + this.ddlParent.SelectedItem.Text + "】下已存在【" + code + "】重复编码！");
                return;
            }

            XDSoft.Model.TB_Options model = new XDSoft.Model.TB_Options();
            model.type = type;
            model.code = code;
            model.name = name;
            model.parent = this.ddlParent.SelectedValue;
            model.sort = sort;
            model.isSys = isSys;

            if (bll.Add(model) > 0)
                Maticsoft.Common.MessageBox.ShowAndRedirect(this, "新增成功！", "add.aspx");
            else
                Maticsoft.Common.MessageBox.Show(this, "新增失败！");

        }
    }
}
